﻿
namespace RIA.ServicesLibrary.Web.Services
{
    using System.Data;
    using System.Linq;
    using System.ServiceModel.DomainServices.EntityFramework;
    using System.ServiceModel.DomainServices.Hosting;
    using RIA.ServicesLibrary.Web.Models;


    // Implements application logic using the AdventureWorksLT_DataEntities context.
    // TODO: Add your application logic to these methods or in additional methods.
    // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
    // Also consider adding roles to restrict access as appropriate.
    // [RequiresAuthentication]
    [EnableClientAccess ()]
    public class SalesOrderService : LinqToEntitiesDomainService<AdventureWorksLT_DataEntities>
    {

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'SalesOrderDetails' query.
        public IQueryable<SalesOrderDetail> GetSalesOrderDetails ()
        {
            return this.ObjectContext.SalesOrderDetails;
        }

        public void InsertSalesOrderDetail ( SalesOrderDetail salesOrderDetail )
        {
            if ( ( salesOrderDetail.EntityState != EntityState.Detached ) )
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState ( salesOrderDetail, EntityState.Added );
            }
            else
            {
                this.ObjectContext.SalesOrderDetails.AddObject ( salesOrderDetail );
            }
        }

        public void UpdateSalesOrderDetail ( SalesOrderDetail currentSalesOrderDetail )
        {
            this.ObjectContext.SalesOrderDetails.AttachAsModified ( currentSalesOrderDetail, this.ChangeSet.GetOriginal ( currentSalesOrderDetail ) );
        }

        public void DeleteSalesOrderDetail ( SalesOrderDetail salesOrderDetail )
        {
            if ( ( salesOrderDetail.EntityState == EntityState.Detached ) )
            {
                this.ObjectContext.SalesOrderDetails.Attach ( salesOrderDetail );
            }
            this.ObjectContext.SalesOrderDetails.DeleteObject ( salesOrderDetail );
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'SalesOrderHeaders' query.
        public IQueryable<SalesOrderHeader> GetSalesOrderHeaders ()
        {
            return this.ObjectContext.SalesOrderHeaders;
        }

        public void InsertSalesOrderHeader ( SalesOrderHeader salesOrderHeader )
        {
            if ( ( salesOrderHeader.EntityState != EntityState.Detached ) )
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState ( salesOrderHeader, EntityState.Added );
            }
            else
            {
                this.ObjectContext.SalesOrderHeaders.AddObject ( salesOrderHeader );
            }
        }

        public void UpdateSalesOrderHeader ( SalesOrderHeader currentSalesOrderHeader )
        {
            this.ObjectContext.SalesOrderHeaders.AttachAsModified ( currentSalesOrderHeader, this.ChangeSet.GetOriginal ( currentSalesOrderHeader ) );
        }

        public void DeleteSalesOrderHeader ( SalesOrderHeader salesOrderHeader )
        {
            if ( ( salesOrderHeader.EntityState == EntityState.Detached ) )
            {
                this.ObjectContext.SalesOrderHeaders.Attach ( salesOrderHeader );
            }
            this.ObjectContext.SalesOrderHeaders.DeleteObject ( salesOrderHeader );
        }
    }
}


